package l1j.server.server.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.opqlo.NewSystem.ItemLevelUp.ItemLevelUpTable;
import l1j.opqlo.Server.L1World.L1World;
import l1j.server.DatabaseFactory;
import l1j.server.server.datatables.ItemTable;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.templates.L1Item;
import l1j.server.server.utils.SQLUtil;

public class L1DwarfForElfInventory extends L1Inventory {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	private static Logger _log = Logger.getLogger(L1DwarfForElfInventory.class.getName());

	private final L1PcInstance _owner;

	public L1DwarfForElfInventory(final L1PcInstance owner) {
		this._owner = owner;
	}

	// ＤＢのcharacter_elf_warehouseから削除
	@Override
	public void deleteItem(final L1ItemInstance item) {
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("DELETE FROM character_elf_warehouse WHERE id = ?");
			pstm.setInt(1, item.getId());
			pstm.execute();
		} catch (final SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
		this._items.remove(this._items.indexOf(item));
	}

	// ＤＢのcharacter_elf_warehouseへ登録
	@Override
	public void insertItem(final L1ItemInstance item) {
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con
					.prepareStatement("INSERT INTO character_elf_warehouse SET id = ?, account_name = ?, item_id = ?, item_name = ?, count = ?, is_equipped=0, enchantlvl = ?, is_id = ?, durability = ?, charge_count = ?, remaining_time = ?, last_used = ?, bless = ?, attr_enchant_kind = ?, attr_enchant_level = ?, firemr = ?, watermr = ?, earthmr = ?, windmr = ?, addsp = ?, addhp = ?, addmp = ?, hpr = ?, mpr = ?, SkillType = ?, level=?, 屬性增傷=?, 六屬性加成=?, 破甲=?, 抵擋=?, 額外攻擊=?, 攻擊成功=?, 經驗=?, 爆擊=?, 大怪=?, 小怪=?, 魔防=?,種族增傷=? ,暈眩 =?, 石化 =?, 睡眠 =?, 凍結 =?, 支撐 =?, 暗黑 =?, 暈眩抗性=?, 石化抗性=? , 睡眠抗性=? , 凍結抗性=? , 支撐抗性=? , 暗黑抗性=? , 掉寶率=?,地減傷=?,火減傷=?,水減傷=?,光減傷=?,闇減傷=?,風減傷=?,魔法增傷=?");
			pstm.setInt(1, item.getId());
			pstm.setString(2, this._owner.getAccountName());
			pstm.setInt(3, item.getItemId());
			pstm.setString(4, item.getName());
			pstm.setLong(5, item.getCount());
			pstm.setInt(6, item.getEnchantLevel());
			pstm.setInt(7, item.isIdentified() ? 1 : 0);
			pstm.setInt(8, item.get_durability());
			pstm.setInt(9, item.getChargeCount());
			pstm.setInt(10, item.getRemainingTime());
			pstm.setTimestamp(11, item.getLastUsed());
			pstm.setInt(12, item.getBless());
			pstm.setInt(13, item.getAttrEnchantKind());
			pstm.setInt(14, item.getAttrEnchantLevel());
			//TODO 飾品強化卷軸修正無法存取人物物品資料
			pstm.setInt(15, item.getFireMr());
			pstm.setInt(16, item.getWaterMr());
			pstm.setInt(17, item.getEarthMr());
			pstm.setInt(18, item.getWindMr());
			pstm.setInt(19, item.getaddSp());
			pstm.setInt(20, item.getaddHp());
			pstm.setInt(21, item.getaddMp());
			pstm.setInt(22, item.getHpr());
			pstm.setInt(23, item.getMpr());
			//TODO 飾品強化卷軸修正無法存取人物物品資料
			//TODO 魔法武器卷軸by missu0524
			pstm.setInt(24, item.getSkillType());
			//TODO 魔法武器卷軸by missu0524
			//opqlo 道具升級系統START
			pstm.setInt(25, item.getLevel());
			//opqlo 道具升級系統END
			pstm.setString(26, item.getLvStatus().saveItemSpecialDmg(0));
			pstm.setString(27, item.getLvStatus().saveItemSpecialDmg(1));
			pstm.setInt(28, item.getExtraDmg());
			pstm.setInt(29, item.getExtraDef());
			pstm.setInt(30, item.getModDmg());
			pstm.setInt(31, item.getModHit());
			pstm.setDouble(32, item.getExpUp());
			pstm.setDouble(33, item.getDoubleDmg());
			pstm.setDouble(34, item.getBigDmg());
			pstm.setDouble(35, item.getSmallDmg());
			pstm.setInt(36, item.getaddMr());
			pstm.setString(37, item.getLvStatus().saveItemSpecialDmg(2));
			pstm.setInt(38,item.getStunDice());
			pstm.setInt(39,item.getStoneDice());
			pstm.setInt(40,item.getSleepDice());
			pstm.setInt(41,item.getFreezeDice());
			pstm.setInt(42,item.getSustainDice());
			pstm.setInt(43,item.getBlindDice());
			pstm.setInt(44, item.getStunMr());
			pstm.setInt(45, item.getStoneMr());
			pstm.setInt(46, item.getSleepMr());
			pstm.setInt(47, item.getFreezeMr());
			pstm.setInt(48, item.getSustainMr());
			pstm.setInt(49, item.getBlindMr());
			pstm.setDouble(50, item.getDroprate());
			/**六屬性減傷**/
			final HashMap<Integer , Double>attrdef = item.getLvStatus().getAttrDef();
			final int[]keylist = {1,2,4,5,6,8};
			for(final int key : keylist){
				final Double value;
				if(attrdef.containsKey(key)){
					value = attrdef.get(key);
				}else{
					value =(double) 0;
				}

				switch(key){
				case 1:
					pstm.setDouble(51, value);
					break;
				case 2:
					pstm.setDouble(52, value);
					break;
				case 4:
					pstm.setDouble(53, value);
					break;
				case 5:
					pstm.setDouble(54, value);
					break;
				case 6:
					pstm.setDouble(55, value);
					break;
				case 8:
					pstm.setDouble(56, value);
					break;
				}
		}
			pstm.setDouble(57, item.getMagicDmgMod());
			pstm.execute();
		} catch (final SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}

	}



	// ＤＢのcharacter_itemsの読込
	@Override
	public void loadItems() {
		if(this._items != null){
			this._items.clear();
		}
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("SELECT * FROM character_elf_warehouse WHERE account_name = ?");
			pstm.setString(1, this._owner.getAccountName());
			rs = pstm.executeQuery();

			while (rs.next()) {
				final L1ItemInstance item = new L1ItemInstance();
				final int objectId = rs.getInt("id");
				item.setId(objectId);
				final int itemId = rs.getInt("item_id");
				final L1Item itemTemplate = ItemTable.getInstance().getTemplate(itemId);
				item.setItem(itemTemplate);
				item.setCount(rs.getInt("count"));
				item.setEquipped(false);
				item.setEnchantLevel(rs.getInt("enchantlvl"));
				item.setIdentified(rs.getInt("is_id") != 0 ? true : false);
				item.set_durability(rs.getInt("durability"));
				item.setChargeCount(rs.getInt("charge_count"));
				item.setRemainingTime(rs.getInt("remaining_time"));
				item.setLastUsed(rs.getTimestamp("last_used"));
				item.setBless(rs.getInt("bless"));
				item.setAttrEnchantKind(rs.getInt("attr_enchant_kind"));
				item.setAttrEnchantLevel(rs.getInt("attr_enchant_level"));
				//TODO 飾品強化卷軸
				item.setFireMr(rs.getInt("firemr"));
				item.setWaterMr(rs.getInt("watermr"));
				item.setEarthMr(rs.getInt("earthmr"));
				item.setWindMr(rs.getInt("windmr"));
				item.setaddSp(rs.getInt("addsp"));
				item.setaddHp(rs.getInt("addhp"));
				item.setaddMp(rs.getInt("addmp"));
				item.setHpr(rs.getInt("hpr"));
				item.setMpr(rs.getInt("mpr"));
				//TODO 飾品強化卷軸
				//TODO 魔法武器卷軸by missu0524
				item.setSkillType(rs.getInt("skilltype"));
				//TODO 魔法武器卷軸by missu0524
				//opqlo 道具升級系統START
				final int level = rs.getInt("level");
				if(level > 1){
					item.setLevel(level);
					ItemLevelUpTable.getInstance().getLevelStatus(item);
				}
				//opqlo 道具升級系統END
				item.getLvStatus().setItemSpecialDmg(item , rs.getString("屬性增傷") ,0);
				item.getLvStatus().setItemSpecialDmg(item , rs.getString("六屬性加成") ,1);
				item.getLvStatus().setItemSpecialDmg(item, rs.getString("種族增傷"), 2);
				item.setExtraDmg(rs.getInt("破甲"));
				item.setExtraDef(rs.getInt("抵擋"));
				item.setModDmg(rs.getInt("額外攻擊"));
				item.setModHit(rs.getInt("攻擊成功"));
				item.setExpUp(rs.getDouble("經驗"));
				item.setDoubleDmg(rs.getInt("爆擊"));
				item.setBigDmg(rs.getInt("大怪"));
				item.setSmallDmg(rs.getInt("小怪"));
				item.setaddMr(rs.getInt("魔防"));
				item.setStunDice(rs.getInt("暈眩"));
				item.setStoneDice(rs.getInt("石化"));
				item.setSleepDice(rs.getInt("睡眠"));
				item.setFreezeDice(rs.getInt("凍結"));
				item.setSustainDice(rs.getInt("支撐"));
				item.setBlindDice(rs.getInt("暗黑"));
				item.setStunMr(rs.getInt("暈眩抗性"));
				item.setStoneMr(rs.getInt("石化抗性"));
				item.setSleepMr(rs.getInt("睡眠抗性"));
				item.setFreezeMr(rs.getInt("凍結抗性"));
				item.setSustainMr(rs.getInt("支撐抗性"));
				item.setBlindMr(rs.getInt("暗黑抗性"));
				item.addDroprate(rs.getDouble("掉寶率")); //opqlo 掉寶率
				item.getLvStatus().setAttrDef(1, rs.getDouble("地減傷"));
				item.getLvStatus().setAttrDef(2, rs.getDouble("火減傷"));
				item.getLvStatus().setAttrDef(4, rs.getDouble("水減傷"));
				item.getLvStatus().setAttrDef(5, rs.getDouble("光減傷"));
				item.getLvStatus().setAttrDef(6, rs.getDouble("闇減傷"));
				item.getLvStatus().setAttrDef(8, rs.getDouble("風減傷"));
				item.setMagicDmgMod(rs.getInt("魔法增傷"));
				this._items.add(item);
				L1World.getInstance().storeObject(item);
			}

		} catch (final SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	// ＤＢのcharacter_elf_warehouseを更新
	@Override
	public void updateItem(final L1ItemInstance item) {
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("UPDATE character_elf_warehouse SET count = ? WHERE id = ?");
			pstm.setLong(1, item.getCount());
			pstm.setInt(2, item.getId());
			pstm.execute();
		} catch (final SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
}
